package com.tupu.service.impl.dispatch;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tupu.entity.dispatch.SignalfrequencyData;
import com.tupu.entity.dispatch.SignalfrequencyLevel;
import com.tupu.entity.dto.SignalFrequencyDTO;
import com.tupu.entity.dto.T0122IalmFactionDTO;
import com.tupu.entity.ocs.T0420Almh;
import com.tupu.mapper.ocs.T0122IalmFactionMapper;
import com.tupu.mapper.ocs.T0420AlmhMapper;
import com.tupu.service.dispatch.SignalfrequencyDataService;
import com.tupu.mapper.dispatch.SignalfrequencyDataMapper;
import com.tupu.service.dispatch.SignalfrequencyLevelService;
import com.tupu.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author 15702
 * @description 针对表【tb_signalfrequency_data】的数据库操作Service实现
 * @createDate 2023-11-15 11:18:10
 */
@Slf4j
@Service
public class SignalfrequencyDataServiceImpl extends ServiceImpl<SignalfrequencyDataMapper, SignalfrequencyData> implements SignalfrequencyDataService {
    public static final Map<String, Map<String, String>> importantMap = new HashMap<>();
    public static final String patternStation = "\\S+风电场|\\S{2,}变(?=\\s|/)|[\\u4e00-\\u9fa5]{2,}站|\\S{2,}电厂|\\S{2,}电场|[\\u4e00-\\u9fa5]{2,}厂|\\S{2,}厂站";
    public static final Pattern r = Pattern.compile(patternStation);
    public static final Pattern rStation = Pattern.compile("\\S+站");

    static {
        Map<String, String> ss1 = new HashMap<>();
        ss1.put("低频", "二级频发");
        ss1.put("中频", "一级频发");
        ss1.put("高频", "一级频发");
        importantMap.put("严重", ss1);
        Map<String, String> ss2 = new HashMap<>();

        ss2.put("低频", "三级频发");
        ss2.put("中频", "三级频发");
        ss2.put("高频", "二级频发");
        importantMap.put("重要", ss2);
        Map<String, String> ss3 = new HashMap<>();

        ss3.put("低频", "三级频发");
        ss3.put("中频", "三级频发");
        ss3.put("高频", "三级频发");

        importantMap.put("一般", ss3);


    }

    public static final DateTimeFormatter dfH = DateTimeFormatter.ofPattern("yyyyMMddHH");
    public static final DateTimeFormatter dfD = DateTimeFormatter.ofPattern("yyyyMMdd");
    public static final DateTimeFormatter dfHMS = DateTimeFormatter.ofPattern("HHmmssSSS");
    public static final DateTimeFormatter dfYDNHMS = new DateTimeFormatterBuilder().appendPattern("yyyyMMddHHmmss")
            .appendValue(ChronoField.MILLI_OF_SECOND, 3).toFormatter();

    @Autowired
    private T0420AlmhMapper t0420AlmhMapper;
    @Autowired(required = false)
    private T0122IalmFactionMapper t0122IalmFactionMapper;

    @Autowired
    private SignalfrequencyLevelService signalfrequencyLevelService;


    @Override
    public void countFrequency(LocalDateTime startTime, LocalDateTime endTime) {
        Duration duration = Duration.between(startTime, endTime);
        long days = duration.toDays();
        if (days > 5) {
            for (long i = 0; i < days; i++) {
                LocalDateTime startTime1 = startTime.plusDays(i);
                LocalDateTime endTime1 = startTime.plusDays(i + 1);
                log.info("分批计算频率分析 startTime:{} endTime:{}", startTime1, endTime1);
                this.doCountFrequency(startTime1, endTime1);
            }
        } else {
            this.doCountFrequency(startTime, endTime);
        }
    }

    public void doCountFrequency(LocalDateTime startTime, LocalDateTime endTime) {

        QueryWrapper<T0420Almh> queryWrapper = new QueryWrapper<>();

        queryWrapper.ge("C0420_DATE", startTime.toEpochSecond(ZoneOffset.of("+8")));
        queryWrapper.le("C0420_DATE", endTime.toEpochSecond(ZoneOffset.of("+8")));

        List<T0420Almh> alarmList = t0420AlmhMapper.selectList(queryWrapper);

        String startTimeYmd = startTime.format(dfD);
        String startTimeHms = startTime.format(dfHMS);
        String endTimeYmd = endTime.format(dfD);
        String endTimeHms = endTime.format(dfHMS);
        List<T0122IalmFactionDTO> eventList = t0122IalmFactionMapper.getList(startTimeYmd, startTimeHms, endTimeYmd, endTimeHms);

        List<SignalfrequencyData> signalfrequencyDataList = new ArrayList<>();
        signalfrequencyLevelService.getLevelByTextNoCache("");
        // 告警
        alarmList.forEach(item -> {
            try {
                SignalfrequencyData signalfrequencyData = transformData(item);
                if (signalfrequencyData != null) {
                    signalfrequencyDataList.add(signalfrequencyData);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        // 事件
        eventList.forEach(item -> {
            try {
                SignalfrequencyData signalfrequencyData = transformData(item);
                if (signalfrequencyData != null) {
                    signalfrequencyDataList.add(signalfrequencyData);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        Map<String, SignalfrequencyData> c = signalfrequencyDataList.stream().collect(Collectors.toMap(item -> item.getTimeHour() + item.getStation() + item.getDeviceDesc(), Function.identity(), (existing, duplicate) -> duplicate));
        Map<String, SignalfrequencyData> cD = signalfrequencyDataList.stream().collect(Collectors.toMap(item -> item.getTimeDay() + item.getStation() + item.getDeviceDesc(), Function.identity(), (existing, duplicate) -> duplicate));
        Map<String, Long> dHour = signalfrequencyDataList.stream().collect(Collectors.groupingBy(item -> item.getTimeHour() + item.getStation() + item.getDeviceDesc(), Collectors.counting()));
        Map<String, Long> dDay = signalfrequencyDataList.stream().collect(Collectors.groupingBy(item -> item.getTimeDay() + item.getStation() + item.getDeviceDesc(), Collectors.counting()));
        dHour.forEach((signalId, count) -> {
            c.computeIfPresent(signalId, (k, v) -> {
                int cc = Math.toIntExact(count);
                String actionType = getActionTypeHour(cc);
//                if (StringUtils.isBlank(actionType)) {
//                    return null;
//                }
                // 小时过频不在计算天过频
                cD.remove(v.getTimeDay() + v.getStation() + v.getDeviceDesc());
                v.setSignalId(signalId.trim());
                v.setSignalSpeed(cc);
                v.setActionType(actionType);
                v.setActionTimeType("小时");
                v.setGrading(getGrading(actionType, v.getImportantType()));
                v.setUpdateTime(LocalDateTime.now());

                return v;
            });
        });
        dDay.forEach((signalId, count) -> {
            cD.computeIfPresent(signalId, (k, v) -> {
                int cc = Math.toIntExact(count);
                String actionType = getActionTypeDay(cc);
                if (StringUtils.isBlank(actionType)) {
                    return null;
                }
                v.setSignalId(signalId.trim());
                v.setSignalSpeed(cc);
                v.setActionType(actionType);
                v.setActionTimeType("天");
                v.setGrading(getGrading(actionType, v.getImportantType()));
                v.setUpdateTime(LocalDateTime.now());

                return v;
            });
        });
        this.saveOrUpdateBatch(c.values());
        this.saveOrUpdateBatch(cD.values());
    }


    @Override
    public Page<SignalfrequencyData> page(Long page, Long size) {
        QueryWrapper<SignalfrequencyData> queryWrapper = new QueryWrapper<>();
        Page<SignalfrequencyData> info = new Page<>(page, size);
        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();
        // 当天的凌晨时间
        LocalDateTime startOfToday = LocalDateTime.of(currentTime.toLocalDate(), LocalTime.MIN);
        // 前一天的凌晨时间
        LocalDateTime startOfYesterday = startOfToday.minus(1, ChronoUnit.DAYS);
        // 前一天的最后一秒时间
        LocalDateTime endOfYesterday = startOfYesterday.with(LocalTime.MAX);
        queryWrapper.ge("time", startOfYesterday);
        queryWrapper.le("time", endOfYesterday);
        queryWrapper.isNotNull("action_type");
        queryWrapper.orderByDesc("time");
        baseMapper.selectPage(info, queryWrapper);
        return info;
    }

    @Override
    public List<String> findAllStation() {
        QueryWrapper<SignalfrequencyData> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("distinct station");
        List<SignalfrequencyData> problemLists = this.baseMapper.selectList(queryWrapper);

        List<String> stringList = problemLists.stream().map(SignalfrequencyData::getStation).distinct().collect(Collectors.toList());
        stringList.remove("");
        return stringList;
    }

    @Override
    public List<Map<String, Object>> signalFrequencyWarningSignalTypePercent(String deviceType, String station, String signalType, String actionType, String importantType, LocalDateTime startTime, LocalDateTime endTime) {
        return this.baseMapper.signalFrequencyWarningSignalTypePercent(deviceType, station, signalType, actionType, importantType, startTime, endTime);
    }

    @Override
    public List<Map<String, Object>> signalFrequencyWarningSignalStationTop5(String deviceType, String station, String signalType, String actionType, String importantType, LocalDateTime startTime, LocalDateTime endTime) {
        return this.baseMapper.signalFrequencyWarningSignalStationTop5(deviceType, station, signalType, actionType, importantType, startTime, endTime);
    }

    @Override
    public void signalFrequencyWarningSignalUpdateActionType(SignalfrequencyData data) {
        SignalfrequencyData signalfrequencyData = new SignalfrequencyData();
        signalfrequencyData.setSignalId(data.getSignalId());
        signalfrequencyData.setActionType(data.getActionType());
        this.updateById(signalfrequencyData);
    }

    @Override
    public List<String> signalFrequencyWarningSignalImportantList() {
        return Arrays.asList(
                "一般",
                "重要",
                "严重"
        );
    }

    @Override
    public List<String> signalFrequencyWarningSignalActionList() {
        return Arrays.asList(
                "低频",
                "中频",
                "高频"
        );
    }

    @Override
    public List<String> signalFrequencyWarningSignalTypeList() {
        return Arrays.asList(
                "越限类",
                "设备类"
        );
    }

    @Override
    public void signalFrequencyWarningSignalUpdateImportantTypeType(SignalfrequencyData data) {
        SignalfrequencyData signalfrequencyData = new SignalfrequencyData();
        signalfrequencyData.setSignalId(data.getSignalId());
        signalfrequencyData.setImportantType(data.getImportantType());
        this.updateById(signalfrequencyData);
    }

    @Override
    public List<Map<String, Object>> signalFrequencyWarningSignalDeviceTop5(LocalDateTime startTime, LocalDateTime endTime) {
        return this.baseMapper.signalFrequencyWarningSignalDeviceTop5(startTime, endTime);

    }

    @Override
    public List<Map<String, Object>> signalFrequencyWarningSignalSpeed(String deviceType, String station, String signalType, String actionType, String importantType, LocalDateTime startTime, LocalDateTime endTime) {
        return this.baseMapper.signalFrequencyWarningSignalSpeed(deviceType, station, signalType, actionType, importantType, startTime, endTime);
    }

    @Override
    public void exportExcel(HttpServletResponse response, String deviceName, String station, String actionType, String importantType, String signalType, LocalDateTime startTime, LocalDateTime endTime) throws IOException {
        QueryWrapper<SignalfrequencyData> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(deviceName)) {
            queryWrapper.like("device_name", deviceName);
        }
        if (StringUtils.isNotEmpty(station)) {
            queryWrapper.like("station", station);
        }
        if (StringUtils.isNotEmpty(signalType)) {
            queryWrapper.eq("signal_type", signalType);
        }
        if (StringUtils.isNotEmpty(actionType)) {
            queryWrapper.eq("action_type", actionType);
        }
        if (StringUtils.isNotEmpty(importantType)) {
            queryWrapper.eq("important_type", importantType);
        }
        if (startTime != null) {
            queryWrapper.ge("time", startTime);
        }
        if (endTime != null) {
            queryWrapper.le("time", endTime);
        }
        queryWrapper.isNotNull("action_type");
        queryWrapper.orderByDesc("time");
        // 最多导出10w数据
        queryWrapper.last("limit 100000");

        List<SignalfrequencyData> list = baseMapper.selectList(queryWrapper);
        OutputStream outputStream = response.getOutputStream();
        try {
            FileUtil.setExcelResponseProp(response, "信号频率分析结果");
            //这个实现方式非常简单直接，使用EasyExcel的write方法将查询到的数据进行处理，以流的形式写出即可
            EasyExcel.write(outputStream, SignalFrequencyDTO.class)//对应的导出实体类
                    .excelType(ExcelTypeEnum.XLSX)//excel文件类型，包括CSV、XLS、XLSX
                    .sheet("信号频率分析结果")//导出sheet页名称
                    .doWrite(list); //查询获取的数据集合List<T>，转成excel
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            outputStream.flush();
            outputStream.close();
        }
    }

    @Override
    public Page<SignalfrequencyData> conditionPage(String deviceType, String station, String signalType, String actionType, String importantType, LocalDateTime startTime, LocalDateTime endTime, Long page, Long size) {

        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();
        // 当天的凌晨时间
        LocalDateTime startOfToday = LocalDateTime.of(currentTime.toLocalDate(), LocalTime.MIN);
        // 前一天的凌晨时间
        LocalDateTime startOfYesterday = startOfToday.minus(1, ChronoUnit.DAYS);
        // 前一天的最后一秒时间
        LocalDateTime endOfYesterday = startOfYesterday.with(LocalTime.MAX);

        QueryWrapper<SignalfrequencyData> queryWrapper = new QueryWrapper<>();
        Page<SignalfrequencyData> info = new Page<>(page, size);
        if (null != startTime || null != endTime) {
            queryWrapper.ge("time", startTime);
            queryWrapper.le("time", endTime);
            if (StringUtils.isNotEmpty(deviceType)) {
                queryWrapper.eq("device_type", deviceType);
            }
            if (StringUtils.isNotEmpty(station)) {
                queryWrapper.like("station", station);
            }
            if (StringUtils.isNotEmpty(signalType)) {
                queryWrapper.eq("signal_type", signalType);
            }
            if (StringUtils.isNotEmpty(actionType)) {
                queryWrapper.eq("action_type", actionType);
            }
            if (StringUtils.isNotEmpty(importantType)) {
                queryWrapper.eq("important_type", importantType);
            }
        } else {
            queryWrapper.ge("time", startOfYesterday);
            queryWrapper.le("time", endOfYesterday);
            if (StringUtils.isNotEmpty(deviceType)) {
                queryWrapper.eq("device_type", deviceType);
            }
            if (StringUtils.isNotEmpty(station)) {
                queryWrapper.like("station", station);
            }
            if (StringUtils.isNotEmpty(signalType)) {
                queryWrapper.eq("signal_type", signalType);
            }
            if (StringUtils.isNotEmpty(actionType)) {
                queryWrapper.eq("action_type", actionType);
            }
            if (StringUtils.isNotEmpty(importantType)) {
                queryWrapper.eq("important_type", importantType);
            }
        }
        queryWrapper.isNotNull("action_type");
        queryWrapper.orderByDesc("time");
        baseMapper.selectPage(info, queryWrapper);
        return info;
    }

    @Scheduled(cron = "0 0/30 * * * ? ")
    public void scheduledCount() {
        LocalDateTime startTime = LocalDateTime.now().minusDays(1);
        LocalDateTime endTime = LocalDateTime.now();
        log.info("开始信号频率分析");
        this.countFrequency(startTime, endTime);
        log.info("信号频率分析完成");
    }

    /**
     * 事件数据
     *
     * @param item
     * @return
     */
    private SignalfrequencyData transformData(T0122IalmFactionDTO item) {
        SignalfrequencyData signalfrequencyData = new SignalfrequencyData();

        String station = "";
        String signalType = "设备类";
        String deviceDesc = null;
        String action = null;
        String importantType = "一般";
        String signalName = null;
        LocalDateTime time = LocalDateTime.parse(item.getC0122YMD() + String.format("%0" + 9 + "d", item.getC0122HMSMS()), dfYDNHMS);
        LocalDateTime now = LocalDateTime.now();
        String timeHour = time.format(dfH);
        String timeDay = time.format(dfD);
        Duration duration = Duration.between(time, now);
        if (duration.toMinutes() <= 60) {
            //实时数据1小时内合并统计
            timeHour = now.format(dfH);
        }
        if (duration.toHours() <= 24) {
            //实时数据1天之内合并统计
            timeHour = now.format(dfH);
        }
        if (StringUtils.isNotEmpty(item.getC0122ActionDesc())) {
            Matcher m = rStation.matcher(item.getC0122ActionDesc());
            if (StringUtils.isNotBlank(item.getC0003StationDesc())) {
                station = item.getC0003StationDesc();
            } else if (m.find()) {
                station = m.group();
                //if (station == null) {
                //   station = m.group();
                // }
            }
            String[] ss = item.getC0122ActionDesc().split("\\s+");
            if (ss.length >= 4) {
                deviceDesc = ss[4].trim();
            }
            if (ss.length >= 5) {
                action = ss[5].trim();
            }
            if (StringUtils.isEmpty(deviceDesc)) {
                deviceDesc = station;
            }

            List<SignalfrequencyLevel> levels = signalfrequencyLevelService.getLevelByText(deviceDesc);
            if (levels.size() > 0) {
                SignalfrequencyLevel level = levels.get(0);
                importantType = level.getFrequencyLevel();
                signalName = level.getType();
            }
            if (deviceDesc.length() > 100) {
                deviceDesc = deviceDesc.substring(0, 99);
            }
            signalfrequencyData.setSignalId(timeHour + (StringUtils.isEmpty(deviceDesc) ? station : station + deviceDesc));
            signalfrequencyData.setStation(station);
            signalfrequencyData.setSignalType(signalType);
            signalfrequencyData.setSignalName(signalName);
            signalfrequencyData.setTime(time);
            signalfrequencyData.setTimeDay(timeDay);
            signalfrequencyData.setTimeHour(timeHour);
            signalfrequencyData.setDeviceDesc(deviceDesc);
            signalfrequencyData.setDeviceName(item.getC1211Devdesc());
            signalfrequencyData.setDeviceId(item.getC1211Devid());
            signalfrequencyData.setDeviceType(item.getC1216Devtypedesc());
            signalfrequencyData.setAction(action);
            signalfrequencyData.setImportantType(importantType);
            signalfrequencyData.setDataSourceType("事件");
            return signalfrequencyData;
        }
        return null;
    }

    public SignalfrequencyData transformData(T0420Almh item) {
        SignalfrequencyData signalfrequencyData = new SignalfrequencyData();

        String station = "";
        String signalType = "设备类";
        String deviceDesc = null;
        String importantType = "一般";
        String signalName = "";
        LocalDateTime time = LocalDateTime.ofEpochSecond(item.getC0420Date(), 0, ZoneOffset.of("+8"));
        LocalDateTime now = LocalDateTime.now();
        String timeHour = time.format(dfH);
        String timeDay = time.format(dfD);
        Duration duration = Duration.between(time, now);
        if (duration.toMinutes() <= 60) {
            //1小时内
            timeHour = now.format(dfH);
        }
        if (duration.toHours() <= 24) {
            //1天之内
            timeHour = now.format(dfH);
        }
        if (StringUtils.isNotEmpty(item.getC0420AlmText())) {
            Matcher m = r.matcher(item.getC0420AlmText().replaceFirst("状态变化", ""));
            if (m.find()) {
                station = m.group();
                if (station == null) {
                    station = m.group();
                }
            }
            if (item.getC0420AlmText().contains("越限")) {
                signalType = "越限类";
            }
            if (item.getC0420AlmText().contains("SOE")) {
                String[] ss = item.getC0420AlmText().split(station, 2);
                if (ss.length >= 2) {
                    deviceDesc = ss[1].trim().replaceAll("\\s+SOE时间.*", "");
                }
                if (ss.length >= 2) {
                    deviceDesc = ss[1].trim().replaceAll("\\s+值:.*|\\s+操作员：.*", "");
                }
            } else {
/*                String[] ss = item.getC0420AlmText().split(station, 2);
                if (ss.length >= 2) {
                    deviceDesc = ss[1].trim().replaceAll("\\s+值:.*|\\s+操作员：.*", "");
                }*/
                try {
                    String[] split = item.getC0420AlmText().split("\\s+");
                    if (split.length >= 6) {
                        deviceDesc = split[4] + " " + split[5];
                    }
                } catch (Exception e) {
                    log.error("频率分析解析错误：" + e.getMessage());
                    deviceDesc = "";
                }
            }
            if (StringUtils.isEmpty(deviceDesc)) {
                deviceDesc = station;
            }

            if (StringUtils.isNotEmpty(deviceDesc)) {
                String[] ss = deviceDesc.split("\\s+");
                String deviceName = ss[0];
                if (deviceName.length() > 64) {
                    deviceName = deviceName.substring(0, 64);
                }
                signalfrequencyData.setDeviceName(deviceName);
                signalfrequencyData.setSignalName(deviceName);
                String deviceType = deviceName.replaceAll("通讯中断|通信中断|归复|动作|\\s|关闭|断开|放电状态|一故障录波装置录波启动 恢复|蓄电池欠压|站用变|事故跳闸，重合成功。|重合闸|事故总信号|异常|故障|总告警|告警|无功不平衡量|\\d+kV\\S+?[线站]|\\S+机组[A-Z]+|[A-Za-z0-9_/一二]|#\\d", "");
                signalfrequencyData.setDeviceType(deviceType.length() > 12 ? deviceType.substring(0, 12) : deviceType);
            }

            List<SignalfrequencyLevel> levels = signalfrequencyLevelService.getLevelByText(deviceDesc);
            if (levels.size() > 0) {
                SignalfrequencyLevel level = levels.get(0);
                importantType = level.getFrequencyLevel();
                signalName = level.getType();
            }
            if (deviceDesc.length() > 100) {
                deviceDesc = deviceDesc.substring(0, 99);
            }
            String[] ss = item.getC0420AlmText().split("\\s+");
            // if (ss.length >= 4) {
            //    signalName = ss[4].trim();
            // }
            signalfrequencyData.setSignalId(timeHour + (StringUtils.isEmpty(deviceDesc) ? station : station + deviceDesc));
            signalfrequencyData.setStation(station);
            signalfrequencyData.setSignalType(signalType);
            signalfrequencyData.setTime(time);
            signalfrequencyData.setTimeDay(timeDay);
            signalfrequencyData.setTimeHour(timeHour);
            signalfrequencyData.setDeviceDesc(deviceDesc);
            signalfrequencyData.setImportantType(importantType);
            signalfrequencyData.setDataSourceType("告警");
            return signalfrequencyData;
        }
        return null;
    }

    /**
     * 获取频发类型
     *
     * @param count
     * @return
     */
    public String getActionTypeHour(int count) {
        String result = null;
        if (count > 40) {
            result = "高频";
        } else if (count > 20 && count <= 40) {
            result = "中频";
        } else if (count > 4 && count <= 20) {
            result = "低频";
        }
        return result;
    }

    public String getActionTypeDay(int count) {
        String result = null;
        if (count > 100) {
            result = "高频";
        } else if (count > 50 && count <= 100) {
            result = "中频";
        } else if (count > 10 && count <= 50) {
            result = "低频";
        }
        return result;
    }

    /**
     * 获取定级
     *
     * @param actionType
     * @param importantType
     * @return
     */
    public String getGrading(String actionType, String importantType) {
        return Optional
                .ofNullable(importantMap.get(importantType))
                .map(action -> action.get(actionType))
                .orElse(null);
    }

    public static void main(String[] args) {
        String str = "2023/08/01 00:42:28           越限恢复  母线功率不平衡        鳌头站10kV#1母线无功不平衡量    由越操作上限 到复归    值:     -0.06";
        String pattern = "\\S+站";
        if (str.contains("    ")) {
            String[] ss = str.split("    ");
            if (ss.length > 4) {
                String device = ss[4];

            }

        }

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);
        if (m.find()) {
            System.out.println(m.group());
        }
    }

}